﻿using System.IO;
using System.Web;
using XWG.JWBST.ProcessInterfaces;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace XWG.JWBST.UserLoginProcess
{
    /// <summary>
    /// 用户登录
    /// </summary>
    public class Process : IProcess
    {
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="reader">接收的数据</param>
        /// <param name="context">请求信息</param>
        /// <returns>操作结果</returns>
        public string ProcessData(StreamReader reader, HttpContext context)
        {
            string uid = "";
            //读取提交数据并UrlDecode解码
            string receivemes = HttpUtility.UrlDecode(reader.ReadToEnd());

            //反序列化json对象
            JObject ob = (JObject)JsonConvert.DeserializeObject(receivemes);
            string phoneno = ob["phone"].ToString().Replace("\"", string.Empty);
            string password = ob["password"].ToString().Replace("\"", string.Empty);

            //查询此手机号是否注册
            if (XWG.JWBST.JWBSTBLL.TbBstUserBLL.SelectUser(phoneno) != 0)
            {
                //if (HttpContext.Current.Session["Uid"] == null)
                //{
                    uid = XWG.JWBST.JWBSTBLL.TbBstUserBLL.UserLogin(phoneno, password);
                    HttpContext.Current.Session["Uid"] = uid;
                    if (uid != string.Empty)
                    {
                        XWG.JWBST.JWBSTBLL.TbBstUserBLL.Updatetime(uid);
                        JObject rejson = new JObject();
                        rejson.Add("uid", uid);
                       string json = JsonConvert.SerializeObject(rejson);
                        return json;
                    }
                //}
                //else
                //{
                //    return HttpContext.Current.Session["Uid"].ToString();
                //}
                return "错误：密码错误";
            }
            else
            {
                return "错误：此手机号尚未注册";
            }

        }
    }
}
